package fr.inserm.u915.tabixserver;

import java.io.File;
import java.util.List;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;



import fr.inserm.u915.tabixserver.expr.ExprNode;

public class Instance extends Named
	{
	public static final String GENOMIC_OBJECT="GenomicObject";
	
	public static final String ABSTRACT_CHROM_START_END="AbstractChromStartEnd";
	private Build build;
	private Table table;
	private File path;
	private ExprNode urlExpr;
	private Group group;
	
	public Instance()
		{
		}
	
	public Build getBuild()
		{
		return build;
		}
	
	void setBuild(Build build)
		{
		this.build = build;
		}
	
	void setGroup(Group group)
		{
		this.group = group;
		}
	
	public Group getGroup()
		{
		return group==null?Group.getDefaultGroup():this.group;
		}
	
	public File getPath()
		{
		return path;
		}
	
	void setPath(File path) {
		this.path = path;
		}
	
	public Table getTable()
		{
		return table;
		}
	
	void setTable(Table table)
		{
		this.table = table;
		}
	
	public List<Column> getColumns()
		{
		return getTable().getColumns();
		}
	
	public int size()
		{
		return getColumns().size();
		}
	public Column get(int index)
		{
		return getColumns().get(index);
		}

	void setUrlExpr(ExprNode urlExpr) {
		this.urlExpr = urlExpr;
		}
	
	public ExprNode getUrlExpr() {
		return this.urlExpr==null?getTable().getUrlExpr():this.urlExpr;
		}
	
	

	
	public void writeXsd(XMLStreamWriter w) throws XMLStreamException
		{
		w.writeStartElement(XSD.prefix,"element",XSD.NS);
		w.writeAttribute("name",getId());
		w.writeAttribute("type",TNS.prefix+":"+getTable().getXsdType());
		w.writeAttribute("substitutionGroup",TNS.prefix+":"+Instance.ABSTRACT_CHROM_START_END);
		XSD.writeAnnotation(w, getDescription());
		w.writeEndElement();
		}
	
	
	public void writeSoap(XMLStreamWriter w)throws XMLStreamException
		{
		w.writeStartElement("tableDef");
		
		w.writeStartElement("id");
		w.writeCharacters(this.getId());
		w.writeEndElement();

		w.writeStartElement("label");
		w.writeCharacters(this.getLabel());
		w.writeEndElement();
		
		w.writeStartElement("description");
		w.writeCharacters(this.getDescription());
		w.writeEndElement();
		
		for(int i=0;i< size();++i)
			{
			Column c=get(i);
			if(c.isIgnore()) continue;
			c.writeSoap(w);
			}
		
		w.writeEndElement();
		}
	
	}
